package org.flexthinker.binding.form.support
{
    import org.flexthinker.binding.value.ValueModel;
    import org.flexthinker.binding.value.support.BufferedValueModel;
    import org.flexthinker.binding.value.support.MockBean;

    import org.hamcrest.assertThat;
    import org.hamcrest.object.equalTo;

    /**
     *
     */
    public class AbstractFormModelTest
    {
        private var fm:AbstractFormModel;

        private var bean:MockBean;

        public function AbstractFormModelTest()
        {
        }

        [Before]
        public function setUp():void
        {
            bean = new MockBean();
            fm = new AbstractFormModel( bean, true );
        }

        [Test]
        public function testGetFieldNames():void
        {
            fm.add( "bindableProperty" );
            assertThat( "getFieldNames is wrong [1]", fm.getFieldNames().length, equalTo( 1 ) );
            fm.add( "bindablePropertyWithCustomEvent" );
            assertThat( "getFieldNames is wrong [2]", fm.getFieldNames().length, equalTo( 2 ) );

            assertThat( "getFieldNames has incorrect values on [0]", fm.getFieldNames()[0], equalTo( "bindableProperty" ) );
        }

        [Test]
        public function testFormObjectInitializationAndCommit():void
        {
            bean.bindableProperty = "STRING PROP";
            assertThat( "formObject is wrong", fm.getFormObject(), equalTo( bean ) );
            var vm1:ValueModel = fm.add( "bindableProperty" );
            assertThat( "valueModel for bean's property should contain the same value", vm1.getValue(), equalTo( bean.bindableProperty ) );

            fm.getValueModel( "bindableProperty" ).setValue( "NEW STRING PROP" );
            assertThat( "bean's property should NOT update according to buffered valueModel automatically", bean.bindableProperty,
                        equalTo( "STRING PROP" ) );

            fm.commit();
            assertThat( "bean's prop should update on form's BufferedValueModel.commit", vm1.getValue(), equalTo( bean.bindableProperty ) );
        }


    }
}